<?php
/**
* View a single page using the page_ident and created time, allowing us to
*     see old history pages.
*
* @package folio
* @param int $page_ident
* @param int $created
*/
    define("context", "folio_page_view_old");
    // Run includes
    require("../../../includes.php");

    require_once("../view/page.php");
    require_once("../model/user.php");
    require_once("../model/tag.php");
    require_once("../model/resource.php");
    require_once("../model/artifact.php");
    require_once("../model/decoration.php");

    global $CFG;
    global $PAGE;
    global $FOLIO_CFG;
    global $metatags;

    // Retrieve passed variables
    $page_ident = intval( required_param('page_ident') );
    $created = intval( required_param('created') );
    $revert = intval( required_param('revert') );

    // Look to
    $url = url;

    // Load Page information
    $page = folio_page::SelectWherePageIdent($page_ident);

    $pages = $page->SelectHistory( $page_ident );
    if( !isset( $pages[$created] ) ) {
        // Invalid history id
        trigger_error('Invalid history id ' . $created . ' passed to view_old.php', E_USER_ERROR);
    }

    // If we're supposed to revert, then go ahead and update the
    //        information and redirect via head to the restored version.
    if( $revert ) {

        // Test to make sure that the user has logged on.  Anonymous users are not allowed to revert pages, since this is
        //        thru a GET and not a POST.
        if ( !isloggedin() ) {
            trigger_error('You must be logged in to revert this page.', E_USER_ERROR);
        }

        // Test to see if the user has permission.
        if ( !$page->Permission('update') ) {
            trigger_error('You do not have permission to revert this page.', E_USER_ERROR);
        }

        // Update current with the past text
        $page->body = $pages[$created]->body;
        $page->format_ident = $pages[$created]->format_ident;
        $page->title = $pages[$created]->title;

        // Save
        $page->Update();

        // Redirect
        header("Location: {$url}page/{$page->page_ident}");
        exit();
    }

    // Shorthand, as we won't need the page array or original page anymore.
    $page = $pages[$created];

    unset( $pages );



    // Load User Information
    $user = folio_user::SelectWhereIdent( $page->user_ident );
    if ( !$user ) {
        trigger_error("Invalid username $user->username passed to view.php", E_USER_ERROR);
    }
    // set the current user.
    global $page_owner;
    $page_owner = $user->ident;

    // Load form helper class
    $form = new folio_BaseView();

    // Test permissions.
    if( !$page->Permission('select') ) {

        // We don't have permissions, and so need some sort of title.
        $html_title = $page->title;
        $plain_title = $page->title;
        $body = __gettext('You do not have permission to view this page. Please contact the page\'s' .
                ' owner and ask for the security to be set to <b>Public</b> or <b>Moderated</b>. ' .
                '  You will be able to view this page once that has been done.  If this page belongs ' .
                ' to a community, you may also try to join the community.  Once you are a member, ' .
                ' you will be able to see and edit the page.');
        $created = '';
        $creator = '';
        $oClickToRevert = '';
    } else {
        // Existing page.with permissions

        // Load the Menu Options.
        // Edit

        // Build nice titles
        $plain_title = $page->title;
        $html_title = "<a href='{$url}activity/user/{$user->username}/rss/page+page_comment/all/1:" .
                folio_createhash( $_SESSION['userid'] ) .
                "'><img border=0 src='{$url}mod/folio/image/feed.png' /></a> " .
                folio_PageBreadcrumb::html($page);

        // Build the body fields
        $form->Load( new folio_PageBodyView( $page ));

        // Build link to revert
        if ( $page->Permission('update') ) {
            $oClickToRevert = __gettext('Click this button to revert back to this version');
            $oClickToRevert = "<input type=\"submit\" value=\"{$oClickToRevert}\" />";
        } else {
            $oClickToRevert = '';
        }
        $body = $form->toString();
    }


    // Building the html control.
    $body = <<< END
<form method="post" name="elggform" action="{$url}{$user->username}/page/{$page->page_ident}:{$page->created}/revert">
<input type="hidden" name="page_ident" value="{$page->page_ident}" />
{$oClickToRevert}
</form>
<br/><br/>
{$body}
<br/>
END;

    // Transfer into template & write.
    templates_page_setup();
    $body = templates_draw(array(
                    'context' => 'contentholder',
                    'title' => $html_title,
                    'body' => $body
                ));
    echo templates_page_draw(array(
                    $plain_title, $body
                ));
?>
